﻿using System;
using System.Collections.Generic;
using System.Text;

using DevScope.CartaoDeCidadao;

namespace ConsoleUI
{
  class Program
  {
    static void Main(string[] args)
    {
      EIDPT.Init("");// We pass blank or a specific card reader name, ex: "Gemplus USB Smart Card Reader 0"
      EIDPT.SetSODChecking(false);

      CardType cardType = EIDPT.GetCardType();
	    switch (cardType)
	    {
		    case CardType.TYPE_IAS07:
			    Console.WriteLine("IAS 0.7 card\n");
			    break;
		    case CardType.TYPE_IAS101:
			    Console.WriteLine("IAS 1.0.1 card\n");
			    break;
		    case CardType.TYPE_ERR:
			    Console.WriteLine("Unable to get the card type\n");
			    break;
		    default:
          Console.WriteLine("Unknown card type\n");
          break;
	    }

      Id userInfo = EIDPT.GetID();

      byte[] byInput = System.Text.Encoding.Default.GetBytes(userInfo.DeliveryEntity);
      String deliveryEntity = System.Text.Encoding.UTF8.GetString(byInput);
      Console.WriteLine("Entidade emissora: " + deliveryEntity);
      Console.WriteLine("Nº Cartao: " + userInfo.CardNumber);
      Console.WriteLine("Mãe: " + userInfo.FirstNameMother + " " + userInfo.NameMother);
      Console.WriteLine("Pai: " + userInfo.FirstNameFather + " " + userInfo.NameFather);

      //PteidAddress userAddress = Pteid.GetAddress();
      //Console.WriteLine("Rua: " + userAddress.Street);

      Picture citizenPicture = EIDPT.GetPicture();
      System.IO.FileStream fs = null;
      try
      {
        fs = new System.IO.FileStream("citizen.jp2", System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Read);
        fs.Write(citizenPicture.Bytes, 0, citizenPicture.Bytes.Length);
      }
      catch (Exception ex)
      {
        Console.WriteLine("Error writing picture. Details: " + ex.Message);
      }
      finally
      {
        if (fs != null)
          fs.Close();
        fs = null;
      }

      // Read personal Data
      byte[] fileIn = { 0x3F, 0x00, 0x5F, 0x00, (byte)0xEF, 0x07 };
      byte[] file = EIDPT.ReadFile(fileIn, (byte)0x81);
      String cardNotes = System.Text.Encoding.UTF8.GetString(file);
      Console.WriteLine("Notas: " + cardNotes);

      // Write personal data
      cardNotes = "Hello PT - áàãâäÁÀÃÂÄ éèêëÉÈÊË íìîïÍÌÎÏ óòõôöÓÒÕÔÖ úùûüÚÙÛÜ çÇ ñ";
      //Pteid.WriteFile(fileIn, System.Text.Encoding.UTF8.GetBytes(data), (byte)0x81);

      // Read Pins
      Pin[] pins = EIDPT.GetPINs();
      Console.WriteLine("Number of pins found: " + pins.Length);

      // Certificates
      Certificate[] certificates = EIDPT.GetCertificates();
      Console.WriteLine("Number of certificates found: " + certificates.Length);

      // Read TokenInfo
      TokenInfo tokenInfo = EIDPT.GetTokenInfo();
      Console.WriteLine("Token Label: {0}  Serial: {1}", tokenInfo.Label, tokenInfo.Serial);

      RSAPublicKey cvcRoot = EIDPT.GetCVCRoot();
      RSAPublicKey cardKey = EIDPT.GetCardAuthenticationKey();

      ////////////////////////////////// CVC ////////////////////////////////////
      //// Modify these values for you test card!
      //static String csCvcCertRole01 = "7F2181CD5F37818086B81455BCF0F508CF79FE9CAD574CA631EBC392D78869C4DC29DB193D75AC7E1BB1852AA57FA54C7E7FA97CBB536F2FA384C90C4FF62EAB119156016353AEAFD0F2E2B41BF89CCFE2C5F463A4A30DC38F2B9145DA3F12C40E2F394E7EE606A4C9377253D6E46D7B538B34C712B964F4A20A5724E0F6E88E0D5D1188C39B75A85F383C6917D61A07CFF92106D1885E393F68BA863520887168CE884242ED86F2F80397B42B883D931F8CCB141DC3579E5AB798B8CCF9A189B83B8D0001000142085054474F56101106"; // CVC cert for writing
      //static String csCvcCertRole02 = "7F2181CD5F3781804823ED79D2F59E61E842ABE0A58919E63F362C9133E873CA77DD79AD01009247460DFE0294DD0ABAABE1D262E69A165F2F1AC6E953E8ABBE3BF1D2ACD6EB69EE83AB918D6F5116589BE0D40E780D5635238B78AA4290AD32F2A6316D24B417E06591DE6A775C38CFD918CA4FD11146EA20E06FE7F73CA7B3D3058FA259745D875F383C6917D61A07CFF92106D1885E393F68BA863520887168CE884242ED86F2F80397B42B883D931F8CCB141DC3579E5AB798B8CCF9A189B83B8D0001000142085054474F56101106"; // CVC cert for reading
      //static String csCvcMod = "924557F6E1C2F1898B391D9255CC72FD7F11128BA148CFEBD1F58AF3F363778157E262FD72A76BCCA0AB43D8F5272E00D21B8B0EE4CC7DA86C8189DEC0DDC58C6A54A81BCE5E52076917D61A07CFF92106D1885E393F68BA863520887168CE884242ED86F2F80397B42B883D931F8CCB141DC3579E5AB798B8CCF9A189B83B8D"; // private key modulus
      //static String csCvcExp = "3B35A8CAFE4E6C79D20AB7C6C1C67611D97AEEB7E8FCD175D353030187578F4BA368B7CB82BAF4EF2B66C89B2D79C3AC7F60B8E4B98771A258F202FE51B23441EB29C68569B608EF1F4B3CF15C68744AA7A3800E364739D3C6DCB078EFB81EA3197C843EE17BD9BCF1E0FEB4FFB6719F923C63105206A2F5A77A0437D762E781"; // private key exponent


      EIDPT.Exit(ExitMode.LEAVE_CARD);
      Console.ReadLine();
    }
  }
}
